<?php

namespace App\controller;

use App\model\Mail;
use support\Request;
use support\Response;

class MailController extends CommonController
{
    /**
     * 邮件列表
     * @param Request $request
     * @return Response
     */
    public function list(Request $request)
    {
        $data = $request->postData;

        // print_r($data);
        $uuid = $data['uuid'] ?? '';
        $limit = $data['limit'] ?? 5;
        $page = $data['page'] ?? 1;
        if (empty($uuid)) {
            return outJson(1, '参数为空');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['is_del'] = 0;
        $query = Mail::query()->where($where_cond)->orderByDesc('created_at');
        $paginator = $query->paginate($limit, ['*'], 'page', $page);
        $lists = $paginator->items();

        $_list = [];
        foreach ($lists as $index => $list) {
            $_list[] = [
                'id' => $list['id'],
                'title' => $list['title'],
                'content' => $list['content'],
                'created_at' => $list['created_at'],
                'is_read' => $list['is_read'],
                'is_receive' => $list['is_receive'],
                'has_attach' => $list['attach'] ? 1 : 0,
                'attach' => $list['attach'],
                'source' => '系统',
            ];
        }
        $out_data['page'] = intval($page);
        $out_data['total'] = $paginator->total();
        $out_data['list'] = $_list;
        return outJson(0, 'ok', $out_data);
    }

    /**
     * 邮件详情
     * @param Request $request
     * @return Response
     */
    public function read(Request $request)
    {
        $data = $request->postData;
        $mail_id = $data['id'] ?? 0;

        if (empty($mail_id)) {
            return outJson(1, '参数为空');
        }

        $where_cond['id'] = $mail_id;
        $where_cond['is_del'] = 0;
        $mail_model = Mail::query()->where($where_cond)->first();
        if (empty($mail_model)) {
            return outJson(1, '邮件不存在');
        }

        //设置为已读
        $mail_model->is_read = 1;
        $mail_model->save();

        return outJson(0, 'ok', ['id' => $mail_id]);
    }

    /**
     * 一键领取
     * @param Request $request
     * @return Response
     */
    public function batchReceive(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $role_id = $data['role_id'] ?? 0;
        $receive_mail_ids = $data['receive_mail_ids'] ?? '';
        if (empty($uuid) || empty($role_id)) {
            return outJson(1, '参数为空');
        }
        if ($receive_mail_ids) {
            $receive_mail_ids = explode(',', $receive_mail_ids);
            $lists = Mail::query()->whereIn('id', $receive_mail_ids)->get();
            foreach ($lists as $model) {
                $model->is_receive = 1;
                $model->save();
            }
        }

        return outJson(0, 'ok');
    }

    /**
     * 领取
     * @param Request $request
     * @return Response
     */
    public function receive(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        $mail_id = $data['mail_id'] ?? '';
        if (empty($uuid)) {
            return outJson(1, '参数为空');
        }
        if ($mail_id) {
            $model = Mail::query()->where('id', $mail_id)->first();
            $model->is_receive = 1;
            $model->save();
        }

        return outJson(0, 'ok', ['id' => $mail_id]);
    }


    /**
     * 删除邮件
     * @param Request $request
     * @return Response
     */
    public function del(Request $request)
    {
        $data = $request->postData;
        $mail_id = $data['id'] ?? 0;
        if (empty($mail_id)) {
            return outJson(1, '参数为空');
        }

        $mail_model = Mail::query()->where('id', $mail_id)->first();
        if (empty($mail_model)) {
            return outJson(1, '邮件不存在');
        }

        if (empty($mail_model['is_read'])) {
            return outJson(1, '未读邮件不能删除');
        }

        if ($mail_model['type'] != 1 and !$mail_model['is_receive']) {
            return outJson(1, '附件未领取，无法删除');
        }

        $mail_model->is_del = 1;
        $mail_model->save();
        return outJson(0, 'ok');
    }

    /**
     * 清理已读，已领取邮件
     * @param Request $request
     * @return Response
     */
    public function batchDel(Request $request)
    {
        $data = $request->postData;
        $uuid = $data['uuid'] ?? '';
        if (empty($uuid)) {
            return outJson(1, '参数为空');
        }

        $where_cond['uuid'] = $uuid;
        $where_cond['is_del'] = 0;
        // $where_cond['is_receive'] = 1;
        $where_cond['is_read'] = 1;
        $lists = Mail::query()->where($where_cond)->get();
        print_r($lists->toArray());
        foreach ($lists as $mail_model) {
            $is_del = false;
            $has_attach = $mail_model->attach ? 1 : 0;
            if ($has_attach == 0) {
                $is_del = true;
            } else {
                if ($mail_model->is_receive == 1) {
                    $is_del = true;
                }
            }

            if ($is_del) {
                $mail_model->is_del = 1;
                $mail_model->save();
            }
        }
        return outJson(0, 'ok');
    }

    /**
     * 记录批量领取id
     * @param Request $request
     * @return Response
     */
    public function recordId(Request $request)
    {
        $data = $request->postData;
        $ids = $data['ids'] ?? '';
        if (empty($ids)) {
            return outJson(1, '参数为空');
        }

        $ids = explode(',', $ids);

        foreach ($ids as $id) {
            $model = Mail::query()->where('id', $id)->first();
            if ($model) {
                $model->is_receive = 1;
                $model->is_read = 1;
                $model->save();
            }
        }
        return outJson(0, 'ok');
    }


}
